package top.jnaw.jee.platform.model;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import java.util.Date;
import java.util.List;
import top.jnaw.jee.platform.Consts;
import top.jnaw.jee.platform.Consts.Result;
import top.jnaw.jee.platform.model.base.BaseFeedback;
import top.jnaw.jee.utils.Dates;
import top.jnaw.jee.utils.ModelResult;
import top.jnaw.jee.utils.Models;
import top.jnaw.jee.utils.Strings;
import top.jnaw.jee.utils.wechat.Wechats;

/**
 * Generated by JFinal.
 */
@SuppressWarnings("serial")
public class Feedback extends BaseFeedback<Feedback> {

  public static final Feedback dao = new Feedback().dao();

  public static ModelResult add(int uid, String title, String version,
      String content, JSONArray images) {
    ModelResult result = new ModelResult(Consts.Result.kModelSaveError);
    Feedback feedback = new Feedback();
    feedback.setUid(uid);
    feedback.setTitle(title);
    feedback.setVersion(version);
    feedback.setContent(content);
    boolean r = feedback.save();
    if (r) {
      if (null != images && images.size() > 0) {
        r = FileModel.add(5, feedback.getId(), images, "accessory");
      }
      result.setObject(feedback);
      result.setResult(r ? Result.kSuccess : Result.kNext);
    }
    return result;
  }

  public static boolean addReply(int fid, String content) {
    boolean r = false;
    Feedback feedback = dao.findById(fid);
    if (fid > 0 && StrKit.notBlank(content)) {
      FeedbackReply feedbackReply = new FeedbackReply();
      feedbackReply.setFid(fid);
      feedbackReply.setContent(content);
      r = feedbackReply.save();
    }
    if (r && null != feedback) {
      List<Users> list = Users.dao
          .find("select * from users where id=" + feedback.getUid());
      String title = "反馈提醒";
      String alert = String
          .format(Consts.NIM_NOTIFY_TEMPLATE_REPLY, "平台管理员", "回复", "意见反馈");

      String from = "feedback";
      String from_id = feedback.getId().toString();

      MessagePush messagePush = new MessagePush(title, alert, from,
          "", from_id, "", "", list);

      messagePush.sendJPush();
      messagePush.sendNIM();
    }
    return r;
  }

  public static boolean delete(int id) {
    return dao.deleteById(id);
  }

  public static JSONObject list(int page, int size, JSONArray filter, int uid) {
    boolean is_admin = false;
    String sec = Db.getSql("feedback-list-sec");
    String exp = Db.getSql("feedback-list-exp");
    Object f = Strings.get(filter, "is_admin", false);
    if (null != f) {
      is_admin = (boolean) f;
    }
    if (!is_admin) {
      exp += " AND f.uid = " + uid;
    }
    exp += " order by create_time desc ";
    Page<Record> pages = Db.paginate(page, size, sec, exp);
    return Models.compressPage(pages);
  }

  public static ModelResult query(int id) {
    ModelResult result = new ModelResult(Result.kNull);
    String key = "feedback-query";
    List<Record> list = Db.find(Db.getSql(key), id);
    if (null != list && list.size() > 0) {
      JSONObject object = Models.compressList(list);
      object.put("image", FileModel.list(5, id, "accessory", false));
      result.setObject(object);
      result.setResult(Consts.Result.kSuccess);
    }
    return result;
  }
}